package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户注册
    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody User user) {
        if (userService.register(user)) {
            return ResponseEntity.ok("注册成功");
        }
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("用户名已存在");
    }

    // 用户登录
    @PostMapping("/login")
    public ResponseEntity<Object> login(@RequestBody Map<String, String> credentials) {
        String username = credentials.get("username");
        String password = credentials.get("password");

        User user = userService.login(username, password);
        if (user == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名或密码错误");
        }

        // 直接返回用户信息Map
        Map<String, Object> data = new HashMap<>();
        data.put("userId", user.getUId());
        data.put("username", user.getUName());
        data.put("isAdmin", user.getUIsAdmin());


        return ResponseEntity.ok(data);
    }

    // 删除用户
    @DeleteMapping("/delete/{userId}")
    public ResponseEntity<String> deleteUser(@PathVariable Integer userId) {
        if (userService.deleteUser(userId)) {
            return ResponseEntity.ok("用户删除成功");
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("用户不存在");
    }

    // 更新用户信息
    @PutMapping("/update/{userId}")
    public ResponseEntity<String> updateUser(
            @PathVariable Integer userId,
            @RequestBody User updatedUser) {
        if (userService.updateUser(userId, updatedUser)) {
            return ResponseEntity.ok("用户信息更新成功");
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("用户不存在");
    }

    // 获取用户信息
    @GetMapping("/info/{userId}")
    public ResponseEntity<User> getUserInfo(@PathVariable Integer userId) {
        User user = userService.getUserById(userId);
        if (user == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(user);
    }

    // 更新密码
    @PutMapping("/updatePassword")
    public ResponseEntity<String> updatePassword(
            @RequestBody Map<String, String> request) {
        Integer userId = Integer.parseInt(request.get("userId"));
        String oldPassword = request.get("oldPassword");
        String newPassword = request.get("newPassword");

        boolean success = userService.updatePassword(userId, oldPassword, newPassword);
        if (success) {
            return ResponseEntity.ok("密码更新成功");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("原密码错误或用户不存在");
        }
    }

    // 账户充值
    @PutMapping("/recharge")
    public ResponseEntity<User> recharge(
            @RequestBody Map<String, Object> request) {
        Integer userId = (Integer) request.get("userId");
        BigDecimal amount = new BigDecimal(request.get("amount").toString());

        try {
            User updatedUser = userService.addBalancePI(userId, amount);
            return ResponseEntity.ok(updatedUser);
        } catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
    }
}
